package com.supermap.localization.database;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.supermap.localization.commontypes.LocationInfo;
import com.supermap.localization.commontypes.SampleInfo;

public class LocalizationDaoImpl implements LocalizationDao {

    private SqlSession sqlSession;

    public LocalizationDaoImpl(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public void insertSample(SampleInfo info) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", null);
        paramMap.put("sampleInfo", info);
        sqlSession.insert("Localization.insertSample", paramMap);
        List<LocationInfo> locationInfos = info.getLocation();
        for (int i = 0; i < locationInfos.size(); i++) {
            LocationInfo currentLocation = locationInfos.get(i);
            Map<String, Object> locationParamMap = new HashMap<>();
            locationParamMap.put("sampleid", paramMap.get("id"));
            locationParamMap.put("location", currentLocation);
            sqlSession.insert("Localization.insertSampleLocation", locationParamMap);
        }
    }

    @Override
    public void insertTranslate(SampleInfo info) {
        int count = sqlSession.selectOne("Localization.getTranslateCount", info.getKey());
        if (count > 0) {
            sqlSession.update("Localization.updateTranslate", info);
        } else {
            sqlSession.insert("Localization.insertTranslate", info);
        }
    }

    @Override
    public List<SampleInfo> selectTranslate() {
        return sqlSession.selectList("Localization.selectTranslate");
    }

}
